package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j

public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //创建ShoppingCart对象用来接收DTO的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        //使用BeanUtils把DTO工具类的值赋值给实体类
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //BaseContext.getCurrentId()获取当前用户的id，并传递到shoppingCart实体类中
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList!=null && shoppingCartList.size()==1){
            //如果在购物车中，则令number字段加一

           shoppingCart = shoppingCartList.get(0);
           //令number字段加一
           shoppingCart.setNumber(shoppingCart.getNumber()+1);
           //修改number字段
            shoppingCartMapper.updateNumberById(shoppingCart);
        } else {
            //如果商品不再购物车中，则判断商品是套餐还是菜品，载分别调用套餐或菜品的查询进行数据回显
            // 判断字段中是否含有dishID有则是菜品，无则是套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null){
                //添加到购物车的是菜品，进行菜品的查询，进行反显
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //添加到购物车当中的是菜品
                Setmeal setmeal = setmealMapper.getById(dishId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    //查询购物车
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    //清空购物车
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
